﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using AutoCode.Win;

namespace EZ.Open.AutoCode
{
    public class EdmxHelper
    {
        public void Format(string filename)
        {
            string filepath = Config.Dir + "/EZ.Open.SQLServerDAL/" + filename;
            string filedesigner = Config.Dir + "/EZ.Open.SQLServerDAL/" + filename.Replace(".edmx", "") + ".Designer.cs";

            string lastline = "";
            List<string> lines = new List<string>();
            var fs = File.ReadAllLines(filedesigner, Encoding.UTF8);
            foreach (var line in fs)
            {
                string l = line;

                if (lastline != "")
                {
                    var r = new OutRelation().GetOutRelation2(lastline);
                    var p = new ProperyInfo().GetInfo(line);
                    l = p.ToInfo(r);
                }

                if (line.Contains("EdmRelationshipNavigationPropertyAttribute"))
                    lastline = line;
                else
                    lastline = "";

                lines.Add(l);
            }
            string result = string.Join("\n" , lines.ToArray());
            File.WriteAllLines(filedesigner,lines.ToArray(),Encoding.UTF8);

            lines = new List<string>();fs = File.ReadAllLines(filepath, Encoding.UTF8);
            foreach (var line in fs)
            {
                string l = line;
                if (line.Contains("NavigationProperty"))//NavigationProperty
                {
                    if (line.Contains("Name=\"EZ_MissionAllot\" Relationship=\"YZZNModel.FK_EZ_MissionAllot_EZ_User_AllotUserID\""))
                    { 
                    }
                    l = new NavigationPropertyInfo().GetInfo(line).ToInfo(line);
                }
                lines.Add(l);
            }
             result = string.Join("\n", lines.ToArray());
            File.WriteAllLines(filepath,lines.ToArray(),Encoding.UTF8);
        }
    }

    public class OutRelation
    {
        public string Table { get; set; }
        public string OutTable { get; set; }
        public string TableRelationFiled { get; set; }

         public OutRelation GetOutRelation(string line)
        {
            //FK_EZ_MissionAllot_EZ_User_SharerUserID
             //MissionAllot_User_SharerUserID
            string[] ls = line.Trim().Trim('\"').Replace("FK_","").Replace("EZ_","").Split('_');
            if (ls.Length >= 1)
                Table = ls[0];
            if (ls.Length >= 2)
                OutTable = ls[1];
            if (ls.Length >= 3)
                TableRelationFiled = ls[2];
            return this;
        }

         public OutRelation GetOutRelation2(string line)
         {
             int begin = line.IndexOf('('); int end = line.IndexOf(')');
             string[] ss = line.Split(',');
             return GetOutRelation(ss[1]);
         }
    }

    public class ProperyInfo
    {
        public string Propery{get;set;}
        public string[] Lines {get;set;}

        public ProperyInfo GetInfo(string line)
        {
            this.Lines = line.TrimEnd(' ').Split(' ');
            this.Propery = Lines[Lines.Length - 1];
            return this;
        }

        public void CheckPropery(OutRelation r)
        {
            var p = this.Propery.Replace("EZ_","");
            if (!string.IsNullOrEmpty(r.TableRelationFiled))//(r.Table != p && r.OutTable != p)
            {
                if (this.Propery.Contains(r.Table))
                    this.Propery = "EZ_"+ r.Table + "Of" + r.TableRelationFiled;
                else
                    this.Propery = "EZ_" + r.OutTable + "Of" + r.TableRelationFiled;
            }
        }

        public string ToInfo(OutRelation r)
        {
            CheckPropery(r);
            Lines[Lines.Length - 1] = Propery;
            return string.Join(" ",Lines);
        }
    }

    public class NavigationPropertyInfo
    {
        public string Name { get; set; }
        public string Relationship { get; set; }
        string tag1 = "Name=\""; string tag2 = "\" Relationship=\""; string tag3 = "\" FromRole=\"";

        public NavigationPropertyInfo GetInfo(string line)
        {
            int begin = line.IndexOf(tag1)+tag1.Length; int end = line.IndexOf(tag2);
            Name = line.Substring(begin, end-begin);
            begin = end + tag2.Length; end = line.IndexOf(tag3);
            var t = line.Substring(begin,end-begin);
            begin = t.IndexOf(".") + 1; end = t.Length;
            Relationship = t.Substring(begin,end-begin);
            return this;
        }

        public string ToInfo(string line)
        {
            OutRelation r = new OutRelation().GetOutRelation(Relationship);
            var p = new ProperyInfo(); p.Propery = Name; p.CheckPropery(r);
            return line.Replace("Name=\"" + Name, "Name=\"" + p.Propery);
        }

    }
}
